<template>
  <div>
    <Card >
      <Row v-show="openSearch" @keydown.enter.native="handleSearch">
        <Form ref="searchForm" :model="searchForm" inline :label-width="70">

          <FormItem label="昵称" prop="realName">
            <Input
                type="text"
                v-model="searchForm.realName"
                placeholder="请输入真实姓名"
                clearable
                style="width: 200px"
            />
          </FormItem>
          <span v-if="drop">
            <FormItem label="身份证号码" prop="idcard">
              <Input
                  type="text"
                  v-model="searchForm.idcard"
                  placeholder="身份证号码"
                  clearable
                  style="width: 200px"
              />
            </FormItem>
          </span>
          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search"
            >搜索</Button
            >
            <Button @click="handleReset">重置</Button>
            <a class="drop-down" @click="dropDown">
              {{ dropDownContent }}
              <Icon :type="dropDownIcon"></Icon>
            </a>
          </FormItem>
        </Form>
      </Row>
      <Row class="operation">
        <Button @click="add" type="primary" icon="md-add">添加</Button>
        <Button @click="getDataList" icon="md-refresh">刷新</Button>
        <Button type="dashed" @click="openTip=!openTip">{{openTip ? "关闭提示" : "开启提示"}}</Button>
      </Row>
      <Alert show-icon v-show="openTip" style="margin-top: 8px">
        已选择
        <span class="select-count">{{ selectList.length }}</span> 项
        <a class="select-clear" @click="clearSelectAll">清空</a>
      </Alert>
      <Table
          :loading="loading"
          border
          :columns="columns"
          :data="data"
          ref="table"
          sortable="custom"
          @on-sort-change="changeSort"
          @on-selection-change="changeSelect"
      >
        <template #action="{ row, index }">
          <Dropdown transfer @click="action(row)">
            <a href="javascript:void(0)">
              操作
              <Icon type="ios-arrow-down"></Icon>
            </a>
            <template #list>
              <DropdownMenu >
                <DropdownItem  @click.native="inverst(row)" ><sapn>充值</sapn></DropdownItem>
                <DropdownItem  @click.native="edit(row)" ><sapn>编辑</sapn></DropdownItem>
                <DropdownItem @click.native="remove(row)" style="color: #ed4014">删除</DropdownItem>
              </DropdownMenu>
            </template>
          </Dropdown>
        </template>
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
            :current="searchForm.pageNumber"
            :total="total"
            :page-size="searchForm.pageSize"
            @on-change="changePage"
            @on-page-size-change="changePageSize"
            :page-size-opts="[10, 20, 50]"
            size="small"
            show-total
            show-elevator
            show-sizer
        ></Page>
      </Row>
    </Card>
    <add-edit
      :data="form"
      :type="showType"
      v-model="visible"
      @close="getDataList"
    ></add-edit>
    <inverst
      :data="invform"
      v-model="invVisible"
      @close="getDataList"></inverst>
  </div>
</template>

<script>
import {page} from '@/api/vip/vipCard.js';
import addEdit from './addEdit'
import inverst from "./inverst";
import {deleteVipCard} from "../../../api/vip/vipCard";

export default {
  name: "index",
  components: {
    addEdit,
    inverst
  },
  data(){
    return {
      openSearch: true,//显示搜索
      openTip: true, //显示提示
      drop: false,
      dropDownContent: '展开',
      dropDownIcon: "ios-arrow-down",
      searchForm: {
        realName: '',
        idcard: '',
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        sort: "createTime", // 默认排序字段
        order: "desc", // 默认排序方式
      },
      data: [], // 表单数据
      total: 0, // 表单数据总数
      loading: false,
      selectList: [], // 多选数据
      columns:  [
        {
          type: "index",
          width: 60,
          align: "center",
        },
        {
          title: "会员卡id",
          key: "cardId",
          minWidth: 120,
          sortable: false,
          align: "center",
        },{
          title: "姓名",
          key: "realName",
          minWidth: 120,
          sortable: false,
          align: "center",
        },{
          title: "身份证号码",
          key: "idcard",
          minWidth: 120,
          sortable: false,
          align: "center",
        },{
          title: "生日",
          key: "birth",
          minWidth: 120,
          sortable: false,
          align: "center",
        },{
          title: "余额",
          key: "cardMoney",
          minWidth: 120,
          sortable: false,
          align: "center",
        },{
          title: "操作",
          slot: "action",
          minWidth: 80,
          align: "center",
        }],
      form: {},
      invform: {},
      showType: "0",
      visible: false,
      invVisible: false,
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getDataList();
    },
    getDataList(){
      this.loading = true;
      page(this.searchForm).then(res => {
        this.loading = false;
        if(res.success){
          this.data = res.result.records;
          this.total = res.result.total;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      })
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    dropDown() {
      if (this.drop) {
        this.dropDownContent = "展开";
        this.dropDownIcon = "ios-arrow-down";
      } else {
        this.dropDownContent = "收起";
        this.dropDownIcon = "ios-arrow-up";
      }
      this.drop = !this.drop;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.selectDate = null;
      this.searchForm.startDate = "";
      this.searchForm.endDate = "";
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    changeSelect(e) {
      this.selectList = e;
    },
    add(){
      this.showType = "2";
      this.visible = true;
    },
    edit(row){
      this.showType = "1";
      this.form = row;
      this.visible = true;
    },
    remove(row){
      this.$Modal.confirm({
        title: "确认删除",
        // 记得确认修改此处
        content: "您确认要删除会员卡 " + row.realName + " ?",
        loading: true,
        onOk: () => {
          // 删除
          deleteVipCard({ id: row.id }).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });

    },
    inverst(row){ // 充值
      this.invform = row;
      this.invVisible = true;
    }
  }
}
</script>

<style lang="less">
@import "@/styles/table-common.less";

</style>
